package com.example.demo7cache.dao;

import com.example.demo7cache.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


public interface PersonRepository extends JpaRepository<Person, Integer> {

    List<Person> findByName(String name);
    List<Person> findByNameLike(String keyword);
    List<Person> findByAge(int age);
    Person findById(int id);
    List<Person> findByNameAndAge(String name, int age);
    List<Person> findByNameLikeAndAge(String name, int age);
    List<Person> findByAgeBetween(int args1, int args2);

    @Query("select p from Person p where p.id=:id")
    Person getUserById(@Param("id") int id);

//    @Modifying  // update 或 delete
//    @Query("update ShopCoupon sc set sc.deleted = true where sc.id in :ids")
//    void deleteByIds(@Param(value = "ids") List<String> ids);

    @Transactional  // 需要加上事务


    @Modifying
    @Query("update Person p set p.name=:name, p.age=:age where p.id=:id")
    void update(@Param("id") int id, @Param("name") String name, @Param("age") int age);

}
